import * as THREE from "three"; 
const gradientShader = {
    uniforms: {
        u_colors: { value: [] },
        u_colorCount: { value: 0 },
    },
    vertexShader: `
        varying vec3 vNormal;
        void main() {
            vNormal = normalize(normalMatrix * normal);
            gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
        }
    `,
    fragmentShader: `
        uniform vec3 u_colors[10]; // Maximum of 10 colors
        uniform int u_colorCount;
        varying vec3 vNormal;

        void main() {
            float gradient = dot(vNormal, vec3(1.0, 0.0, 0.0));
            gradient = (gradient + 1.0) / 2.0;

            vec3 color = u_colors[0]; // 默认使用第一个颜色
            float step = 1.0 / float(u_colorCount - 1);
            
            for (int i = 0; i < 10; i++) {
                if (i >= u_colorCount - 1) break;
                float lowerBound = float(i) * step;
                float upperBound = float(i + 1) * step;
                if (gradient >= lowerBound && gradient < upperBound) {
                    float t = (gradient - lowerBound) / step;
                    color = mix(u_colors[i], u_colors[i + 1], t);
                    break;
                }
            }

            gl_FragColor = vec4(color, 0.9);
        }
    `
};
// 创建渐变色材质
export function createGradientMaterial(colors) {
    const colorArray = new Float32Array(colors.length * 3);
    colors.forEach((color, index) => {
        const threeColor = new THREE.Color(color);
        colorArray[index * 3] = threeColor.r;
        colorArray[index * 3 + 1] = threeColor.g;
        colorArray[index * 3 + 2] = threeColor.b;
    });

    const material = new THREE.ShaderMaterial({
        uniforms: {
            u_colors: { value: colorArray },
            u_colorCount: { value: colors.length },
        },
        vertexShader: gradientShader.vertexShader,
        fragmentShader: gradientShader.fragmentShader,
        transparent: true,
        opacity: 0.9,
    });

    return material;
}
// 创建眼镜背面
export function createBackMaterial() {
    return new THREE.MeshStandardMaterial({
        color: 0x8B4513, // 更深的棕色
        transparent: true,
        opacity: 0.9, // 增加透明度
        metalness: 0.0, // 减少金属感
        roughness: 0.1, // 减少粗糙度
        transmission: 0.1, // 增加透明度
        reflectivity: 0.8, // 增加反射率
        clearcoat: 0.1, // 减少清漆效果
        clearcoatRoughness: 0.01,
    });
}